package eg.com.itiga.ui.actions;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import eg.com.itiga.business.base.BaseService;
import eg.com.itiga.domain.*;
import eg.com.itiga.ui.beans.ManageCompanyActionForm;
import eg.com.itiga.business.factory.BusinessFactory;
import eg.com.itiga.business.role.RoleService;

public class AddCompanyAction extends org.apache.struts.action.Action {
    


    
    public AddCompanyAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	ManageCompanyActionForm frm = (ManageCompanyActionForm)form;
    	
    	
    	RoleService roleService = BusinessFactory.createRoleService();
        Role companyRole = roleService.getRoleByName("Company");
        
        BaseService baseService = BusinessFactory.createBaseService();
    	
        
    	User companyUsr=new User(companyRole,frm.getUserName(),frm.getPassword());
    	
    	RegCompany regCompany = new RegCompany(companyUsr);
    	
    	regCompany.setCName(frm.getCompanyName());
    	regCompany.setCIsRegistered(true);
    	regCompany.setRegWesite(frm.getCompanyWebsite());
    	//regCompany.setRegExpDate(new Date());
    	
    	Set<CompanyField> companyFieldsSet = new HashSet<CompanyField>();
    	
    	
    	
        int[] fieldsIds =  frm.getSelectedFields();
        CompanyField[] compnayFields = new CompanyField[fieldsIds.length];
        Field[] flds = new Field[fieldsIds.length];
        
        for(int i=0;i<fieldsIds.length;i++)
        {
        	flds[i]=(Field)baseService.find(Field.class, fieldsIds[i]);
        	compnayFields[i] = new CompanyField(flds[i],regCompany);
        	companyFieldsSet.add(compnayFields[i]);
        	
        	
        }
        
        regCompany.setCompanyFields(companyFieldsSet);

    	String[] branches=request.getParameterValues("branchName");
    	String[] addresses=request.getParameterValues("address");
    	String[] phones=request.getParameterValues("phone");
    	String[] emails=request.getParameterValues("email");
    	
    	ContactDetails[] contactDetails = new ContactDetails[branches.length];
    	Set<ContactDetails> contactDetailsSet = new HashSet<ContactDetails>();
    	
        if(branches!=null&&addresses!=null&&phones!=null&&emails!=null)
        {
        	for(int i=0;i<branches.length;i++)
        	{
        		//creation of company details DTOs 
        		contactDetails[i] = new ContactDetails(regCompany);
        		contactDetails[i].setConAddress(addresses[i]);
        		contactDetails[i].setConBranch(branches[i]);
        		contactDetails[i].setConEmail(emails[i]);
        		contactDetails[i].setConPhone(phones[i]);
        		contactDetailsSet.add(contactDetails[i]);
        		
        	}
        }
        regCompany.setContactDetailses(contactDetailsSet);
        baseService.add(regCompany);
        System.out.println("company addeddd!!! ya rab...");
        ActionForward forward=mapping.findForward("companyadded");
        return forward;
    }

}